package com.example.controller.sys;
import com.example.pojo.Process;
import com.example.service.IProcessService;
import com.example.vo.ResultVo;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;

import java.sql.Date;
import java.util.Arrays;

/**
 * <p>
 * 流程表 前端控制器
 * </p>
 *
 * @author wsl
 * @since 2022-04-10
 */
@RestController
@RequestMapping("/sys/process")
@Api(tags = "流程管理",value = "")
public class ProcessController {

    @Autowired
    private IProcessService processService;

    @ApiOperation(value = "查询流程")
    @GetMapping("/query")
    public ResultVo getProcessList(
            Integer classifyId,
            String keyword,
            Date start,
            Date end
    ){
        return processService.getProcessList(classifyId,keyword,start,end);
    }

    @ApiOperation(value = "新增流程")
    @PostMapping("/add")
    public ResultVo addProcess(@RequestBody Process process){
        if(processService.save(process)){
            return ResultVo.ok("添加成功");
        }
        return ResultVo.error("添加失败!");
    }

    @ApiOperation(value = "修改流程")
    @PutMapping("/update")
    public ResultVo updateProcess(@RequestBody Process process){
        if(processService.updateById(process)){
            return ResultVo.ok("修改成功");
        }
        return ResultVo.error("修改失败!");
    }

    @ApiOperation(value = "删除流程")
    @DeleteMapping("/delete")
    public ResultVo deleteProcess(Integer[] ids){
        if(processService.removeByIds(Arrays.asList(ids))){
            return ResultVo.ok("删除成功");
        }
        return ResultVo.error("删除失败!");
    }

    @ApiOperation(value = "同类别下是否已有相同流程")
    @GetMapping("/has-same-name-sibling")
    public Boolean hasSameNameSibling(Integer id,@RequestParam(name="name")String name,@RequestParam(name="classifyId")Integer classifyId){
        return processService.hasSmeNameSiblingProcess(id,name,classifyId);
    }

    @ApiOperation(value = "是否已有相同编码的流程")
    @GetMapping("/has-same-code-sibling")
    public Boolean hasSameCodeSibling(Integer id,@RequestParam(name = "code")String code){
        return processService.hasSameCodeSiblingProcess(id,code);
    }
}
